package cn.wang.web.cargo;

import cn.wang.entity.cargo.ContractProduct;
import cn.wang.entity.cargo.Factory;
import cn.wang.service.cargo.ContractProductService;
import cn.wang.service.cargo.ContractService;
import cn.wang.utils.Pagination;
import cn.wang.web.BaseAction;
import com.opensymphony.xwork2.ModelDriven;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import java.util.List;
import java.util.Set;

/**
 * Created by john on 2017/11/1.
 */
@Controller
public class ContractProductAction extends BaseAction implements ModelDriven<ContractProduct> {
    private Pagination page = new Pagination();
    private List<Factory> factoryList;
    private Set<ContractProduct> productSet;
    private ContractProduct model = new ContractProduct();

    @Autowired
    private ContractService contractService;
    @Autowired
    private ContractProductService contractProductService;

    //分页
    public String list() {
        factoryList = contractProductService.findAll();
        page = contractProductService.findPage1(page.getPageNo(), page.getPageSize(), model.getContract().getId());
        page.setUrl("contractProductAction_list");
        push(page);

        return "tocreate";
    }

    //查询所有厂商 分页和添加在同一页 第一次访问tocreate 实现分页 第二访问list()
    public String tocreate() {
        factoryList = contractProductService.findAll();
        page = contractProductService.findPage1(page.getPageNo(), page.getPageSize(), model.getContract().getId());
        page.setUrl("contractProductAction_list");
        push(page);
        return "tocreate";
    }

    //查寻单个部门信息
    public String toview() {
        model = contractProductService.findById(model.getId());
        push(model);
        return "toview";
    }

    //添加产品
    public String insert() {
        contractProductService.save(model);
        return tocreate();

    }

    //更新货物 先查询老数据
    public String toupdate() {
        //根据id查询老shuju1
        factoryList = contractProductService.findAll();
        model = contractProductService.findById(model.getId());
        push(model);
        return "toupdate";
    }

    public String update() {
        contractProductService.update(model);
        return "rlist";
    }

    //删除货物 多个货物删除时 id 字符类型id会 拼接在一起(,空格)
    public String delete() {
        if (model.getId() == null) {
            return "rlist";
        }
        String[] ids = model.getId().split(", ");
        contractProductService.delete(ids,model.getContract().getId());
        return "rlist";
    }

    public void setPage(Pagination page) {
        this.page = page;
    }


    public List<Factory> getFactoryList() {
        return factoryList;
    }

    public void setFactoryList(List<Factory> factoryList) {
        this.factoryList = factoryList;
    }

    @Override
    public ContractProduct getModel() {
        //将字段名设为model使一行代码两用 1模型驱动 2 字段set方法
        return model;
    }
}
